package com.canbot.u05.activity.guide;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.BaseCompereActivity;
import com.canbot.u05.utils.ToastTools;


/**
 * @author zmp
 * 导览模式动作设置
 */
public class GuideActionSetActivity extends BaseCompereActivity implements View.OnClickListener {

        private static final String TAG = "GuideActionSetActivity";

        private String actionID;

        private String actionName;

        private CheckBox defaultActionBox;

        private CheckBox actionCheckBox;

        private static final int REQUEST_CODE = 1001;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_guide_action_set);
                Intent intent = getIntent();
                actionID = intent.getStringExtra("actionID");
                actionName = intent.getStringExtra("actionName");
                initView();
        }

        private void initView() {
                findViewById(R.id.guide_action_set_back).setOnClickListener(this);
                findViewById(R.id.guide_action_set_save).setOnClickListener(this);
                findViewById(R.id.defaultActionFL).setOnClickListener(this);
                findViewById(R.id.actionFL).setOnClickListener(this);
                defaultActionBox = (CheckBox) findViewById(R.id.defaultActionBox);
                actionCheckBox = (CheckBox) findViewById(R.id.actionCheckBox);
                setCheckBox();

        }

        private void setCheckBox() {
                if ((TextUtils.isEmpty(actionID))| ("-1".equals(actionID))) {
                        defaultActionBox.setChecked(true);
                        actionCheckBox.setChecked(false);
                } else {
                        defaultActionBox.setChecked(false);
                        actionCheckBox.setChecked(true);
                }
        }


        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.guide_action_set_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.defaultActionFL:
                                //默认动作
                                defaultActionBox.setChecked(true);
                                actionCheckBox.setChecked(false);
                                break;
                        case R.id.actionFL:
                                //指定动作
                                actionCheckBox.setChecked(true);
                                defaultActionBox.setChecked(false);
                                //跳转到指定动作界面
                                startActionChoiceActivity();
                                break;
                        case R.id.guide_action_set_save:
                                //右上角完成按钮
                                if (defaultActionBox.isChecked()) {
                                        Intent intent = new Intent();
                                        intent.putExtra("actionID", "-1");
                                        intent.putExtra("actionName", "默认动作");
                                        setResult(RESULT_OK, intent);
                                        finish();
                                        return;
                                }
                                if (TextUtils.isEmpty(actionID)) {
                                        ToastTools.newInstance(this).showText(getString(R.string.please_select_action));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("actionID", actionID);
                                intent.putExtra("actionName", actionName);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                        default:

                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
                super.onActivityResult(requestCode, resultCode, intent);
                if (intent != null) {
                        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
                                actionID = intent.getStringExtra("actionID");
                                actionName = intent.getStringExtra("actionName");
                        }
                }
                setCheckBox();
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
        }

        /**
         * 跳转指定动作选择界面
         */
        protected void startActionChoiceActivity() {
                Intent intent = new Intent(this, GuideActionChoiceActivity.class);
                intent.putExtra("actionID", actionID);
                intent.putExtra("actionName", actionName);
                startActivityForResult(intent, REQUEST_CODE);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }
}
